package com.easyadmin.modules.stock.controller;


import com.easyadmin.common.aop.annotation.Repeat;
import com.easyadmin.common.web.base.BaseController;
import com.easyadmin.common.web.domain.request.PageDomain;
import com.easyadmin.common.web.domain.response.Result;
import com.easyadmin.common.web.domain.response.module.ResultTable;
import com.easyadmin.modules.stock.domain.StockDividendEntity;
import com.easyadmin.modules.stock.domain.SysFactorEntity;
import com.easyadmin.modules.stock.domain.SysFuturesEntity;
import com.easyadmin.modules.stock.domain.SysTradeDateEntity;
import com.easyadmin.modules.stock.service.SysFactorServer;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.Date;

/**
 * 因子表
 *
 * @author wanzenghui
 * @email lemon_wan@aliyun.com
 * @date 2025-04-03 20:54:57
 */
@Slf4j
@RestController
@RequestMapping("/stock/factor")
public class SysFactorController extends BaseController {


    @Autowired
    SysFactorServer sysFactorServer;


    @Autowired
    private RestTemplate restTemplate;

    /**
     * Describe: 基础路径
     */
    private static String MODULE_PATH = "stock/factor/";

    /**
     * Describe: 获取角色列表视图
     * Param ModelAndView
     * Return 用户列表视图
     */
    @GetMapping("main")
    @ApiOperation(value = "获取列表视图")
    @PreAuthorize("hasPermission('/stock/factor/main','stock:factor:main')")
    public ModelAndView main() {
        return jumpPage(MODULE_PATH + "main");
    }

    /**
     * Describe: 获取新增视图
     * Param ModelAndView
     * Return 角色新增视图
     */
    @GetMapping("add")
    @ApiOperation(value = "新增视图")
    @PreAuthorize("hasPermission('/stock/factor/add','stock:factor:add')")
    public ModelAndView add() {
        return jumpPage(MODULE_PATH + "add");
    }


    /**
     * 分页查询
     */
    @ResponseBody
    @GetMapping("/list")
    @PreAuthorize("hasPermission('/stock/factor/list','stock:factor:list')")
    public ResultTable list(@ModelAttribute SysFactorEntity sysFactorEntity, PageDomain pageDomain) {
        PageInfo<SysFactorEntity> pageInfo = sysFactorServer.queryPage(sysFactorEntity, pageDomain);

        //http://hq.sinajs.cn/list=s_sh000001,s_sz399001
        //发起请求(get)https://hq.sinajs.cn/list=sh600928,sz301378,sz300111,sz300476,sz159241,sz159760,sz159396
        String marketUrls="https://hq.sinajs.cn/list=sh600928,sz301378,sz300111";
        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36");
        headers.set("Referer", "https://finance.sina.com.cn/"); // 必须
        HttpEntity<String> entity = new HttpEntity<>(headers);
        // 使用exchange方法，可以更好地控制请求
        ResponseEntity<String> response = restTemplate.exchange(
                marketUrls,
                HttpMethod.GET,
                entity,
                String.class
        );
        log.info(response.toString());












        return pageTable(pageInfo.getList(), pageInfo.getTotal());


    }

    /**
     * Describe: 删除某条数据
     * Param: id
     * Return: Result
     */
    @DeleteMapping("/remove/{id}")
    @ApiOperation(value = "删除某条数据")
    @PreAuthorize("hasPermission('/stock/factor/remove','stock:factor:remove')")
    public Result remove(@PathVariable String id) {
        return decide(sysFactorServer.remove(id));
    }

    /**
     * Describe: 保存期货信息
     * Param SysRole
     * Return 执行结果
     */
    @Repeat
    @PostMapping("/save")
    @ApiOperation(value = "保存期货品种数据")
    @PreAuthorize("hasPermission('/stock/factor/add','stock:factor:add')")
    public Result save(@RequestBody SysFactorEntity sysFactorEntity) {
        sysFactorEntity.setCreateTime(new Date());
        return decide(sysFactorServer.save(sysFactorEntity));
    }


    /**
     * Describe: 获取修改视图
     * Param ModelAndView
     * Return 修改视图
     */
    @GetMapping("edit")
    @ApiOperation(value = "获取修改视图")
    @PreAuthorize("hasPermission('/stock/factor/edit','stock:factor:edit')")
    public ModelAndView edit(ModelAndView modelAndView, String id) {
        modelAndView.addObject("stockFactor", sysFactorServer.getById(id));
        modelAndView.setViewName(MODULE_PATH + "edit");
        return modelAndView;
    }


    /**
     * Describe: 修改角色信息
     * Param SysRole
     * Return 执行结果
     */
    @PutMapping("update")
    @ApiOperation(value = "修改数据")
    @PreAuthorize("hasPermission('/stock/factor/edit','stock:factor:edit')")
    public Result update(@RequestBody SysFactorEntity sysDailyTopicThinkEntity) {
        return decide(sysFactorServer.update(sysDailyTopicThinkEntity));
    }

    /**
     * Describe: 批量删除接口
     * Param: ids
     * Return: Result
     */
    @DeleteMapping("batchRemove/{ids}")
    @ApiOperation(value = "批量删除数据")
    @PreAuthorize("hasPermission('/stock/factor/remove','stock:factor:remove')")
    public Result batchRemove(@PathVariable String ids) {
        return decide(sysFactorServer.batchRemove(ids.split(",")));
    }

}
